package com.zxmzy.scaffold.core.widget.slideback

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import androidx.annotation.ColorInt
import com.zxmzy.scaffold.core.ext.dp

/**
 * @author zhangxiaoming
 * @date 2023/8/31
 * @description 默认样式（弹性贝塞尔曲线+箭头）
 */
class DefaultSlideView(private val context: Context) : ISlideView {
    private val bezierPath: Path = Path()
    private val paint: Paint
    private val arrowPaint: Paint
    private var backViewColor: Int = Color.parseColor("#ff000000")
    private var arrowColor = Color.WHITE
    private val arrowWidth: Int = 4.dp()
    private val width: Int = 50.dp()
    private val height: Int = 200.dp()

    init {
        paint = Paint().apply {
            isAntiAlias = true
            style = Paint.Style.FILL
            color = backViewColor
            strokeWidth = 1.5.dp().toFloat()
        }

        arrowPaint = Paint().apply {
            isAntiAlias = true
            style = Paint.Style.FILL_AND_STROKE
            color = arrowColor
            strokeWidth = 1.5.dp().toFloat()
            strokeCap = Paint.Cap.ROUND
        }
    }

    override fun scrollVertical(): Boolean {
        return true
    }

    override fun getWidth(): Int {
        return width
    }

    override fun getHeight(): Int {
        return height
    }

    override fun onDraw(canvas: Canvas, currentWidth: Float) {
        val height = getHeight()
        val maxWidth = getWidth()
        val centerY: Float = height / 2f

        val progress: Float = currentWidth / maxWidth
        if (progress == 0f) return

        paint.color = backViewColor
        paint.alpha = (200 * progress).toInt()

        val bezierWidth: Float = currentWidth / 2
        with(bezierPath) {
            reset()
            moveTo(0f, 0f)
            cubicTo(0f, height / 4f, bezierWidth, height * 3f / 8, bezierWidth, centerY)
            cubicTo(bezierWidth, height * 5f / 8, 0f, height * 3f / 4, 0f, height.toFloat())
        }
        canvas.drawPath(bezierPath, paint)

        arrowPaint.color = arrowColor
        arrowPaint.alpha = (255 * progress).toInt()
        val arrowLeft = currentWidth / 6f
        if (progress <= 0.2f) {
            //ingore
        } else if (progress <= 0.7f) {
            //起初变长竖直过程
            val newProgress = (progress - 0.2f) / 0.5f
            canvas.drawLine(
                arrowLeft,
                centerY - arrowWidth * newProgress,
                arrowLeft,
                centerY + arrowWidth * newProgress,
                arrowPaint
            )
        } else {
            //后面变形到完整箭头过程
            val arrowEnd = arrowLeft + (arrowWidth * (progress - 0.7f) / 0.3f)
            canvas.drawLine(arrowLeft, centerY - arrowWidth, arrowEnd, centerY, arrowPaint)
            canvas.drawLine(arrowEnd, centerY, arrowLeft, centerY + arrowWidth, arrowPaint)
        }

    }

    fun setBackViewColor(@ColorInt backViewColor: Int) {
        this.backViewColor = backViewColor
    }

    fun setArrowColor(@ColorInt arrowColor: Int) {
        this.arrowColor = arrowColor
    }
}